/**
 * Created by Administrator on 2017/2/25.
 */

import {allUrl} from '../scripts/url'
import {Toast,checkLogin,getFormatDateByLong} from '../scripts/tip'

export const  inintTicket =(productId,productDetailId,count) => {
    var sinceId = -10;
    var pageSize = 10;

    $(' .main').dropload(
        {
            scrollArea: window,
            loadDownFn: function (me) {
                sinceId += 10;

                $.ajax({
                    type: 'GET',
                    url: '' + allUrl() + 'mall/queryMyVoucherList?isEnable=0&sinceId=' + sinceId + '&pagesize=' + pageSize ,
                    dataType: 'json',
                    success: function (data) {
                        if(data.head.returnCode===0){
                            var result=[];
                            if(data.body){
                                result=data.body.voucherList;
                            }else {
                                result=[]
                            }
                            getTicketList(result,me,sinceId)
                        }else {
                            Toast(data.head.returnMsg,2000,'');
                        }
                    }
                })
            }
        }
    )
};



function  getTicketList(result,me,sinceId){
    var str='';
    var $length=result.length;

    if($length>0){

        for(var i=0;i<$length;i++){

           var voucherMoney=result[i].voucherMoney;
            var imgUrl='';

            if( voucherMoney<=5){
                imgUrl='images/d_5.png'
            }else if( voucherMoney<=10){
                imgUrl='images/d_10.png'
            }else {
                imgUrl='images/d_20.png'
            }


            str+='<li class="list-item" voucherId="'+result[i].id+'">' +
                '<div class="list-item-wrap">' +
                '<div class="list-image">' +
                '<div class="image">' +
                '<img src="'+imgUrl+'" />' +
                '</div>' +
                '<div class="status">可用</div>' +
                '</div>' +
                '<div class="desc">' +
                '<div class="title">' +
                '<h4>'+result[i].voucherName+'</h4>' +
                '<a href= "javascript:;" class="radioBtn">' +
                '<i class="icon"></i>' +
                '</a>' +
                '</div>' +
                '<div>抵用券：<span class="voucherSecret">'+result[i].voucherSecret+'</span></div>' +
                '<div>有效期：'+getFormatDateByLong(result[i].startTime)+'-'+getFormatDateByLong(result[i].endTime)+'</div>' +
                '</div>' +
                '<div class="clearfix"></div>' +
                '</div>' +
                '</li>'

        }
    }else {
        // 锁定
        me.lock();
        // 无数据
        me.noData();

    }
        // 插入数据到页面，放到最后面

        $('.VoucherTicket ul').append(str);

        if(sinceId===0){

            $('.VoucherTicket ul').find('li:first-child').addClass('select')
        }

        // 每次数据插入，必须重置
        me.resetload();


        $('.list-item').bind('click','.radioBtn',function(){

            $(this).addClass('select').siblings().removeClass('select')
        });
}


export const  testTicket =(productId,voucherSecret,voucherId,productDetailId,count) => {

    $.getJSON(''+allUrl()+'mall/checkVoucher',{
            productId:productId,
            voucherSecret:voucherSecret
    },
        (data) => {

            checkLogin(data, false, '');

            if(data.head.returnCode===0){
                if(data.head.subCode===0){

                    var voucherMoney=data.body.voucherMoney;
                    var voucherSecret=data.body.voucherSecret;

                    location.href='#/mall/presetOrderDetail?productId='+productId+'&voucherMoney='+voucherMoney+'&voucherSecret='+voucherSecret+'&productDetailId='+productDetailId+'&count='+count+'';

                }else{
                    Toast(data.head.subMsg,2000,'');
                }
            }else {
                Toast(data.head.returnMsg,2000,'');
            }
    })
};

